// miniprogram/pages/payment/payment.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      list: [],
      info: [],
      cart_count: '0',
      subtotal: '0',
      total: '0',
      style:"height:600rpx;overflow:hidden",
      discount: '0',
      shipping: '0',
      display: 'none',
      order_num:'',
      wximg:'https://img.quanlaoba.com/skins/base/widgets/process-payment_cn/wxpay.png'
  },

  handelClick: function (e) {
    let display = this.data.display;
    if (display == 'none') {
      this.setData({
        display: 'block',
        style:'height:auto;overflow:hidden'
      })
    } else {
      this.setData({
        display: 'none',
        style:'height:600rpx;overflow:hidden'
      })
    }

  },

  goCart: function (e) {
    wx.switchTab({
      url: '../cart/cart'
    })
  },
  
  getcarts() {
    var that = this;

    app.getUserToken(function (err, token) {
      wx.request({
        url: app.globalData.url +'/index.php?_m=checkout/cart/get_cart',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        method: 'POST',
        data: { auth_token: token },
        success: (res) => {
          if (res.data.status == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.getcarts();
          } else if (res.data.code == 0) {
            that.setData({
              list: res.data.data.products,
              cart_count: res.data.data.cart_count,
              subtotal: res.data.data.subtotal,
              total: res.data.data.total,
              discount: res.data.data.discount.value,
              shipping: res.data.data.shipping.cost,
              order_num: res.data.data.config.description,
              info: res.data.data.info.shipping_address
            })
          }
        },
        fail: (res) => {
          wx.showToast({
            title: '获取借口失败',
            duration: 2000
          })
        }
      })

    })
  },

  goBack:function(e){
    wx.navigateBack({
      delta: 1
    })
  },

  editAddress: function (e) {
    wx.navigateBack({
      delta: 1
    })
  },

  wxpay:function(e){
    let order_num=this.data.order_num;
    var that = this;

    app.getUserToken(function (err, token) {
      wx.showLoading({
        title: '加载中...',
      })
      wx.request({
        url: app.globalData.url +'index.php?_m=checkout/wxpay&order_num=' + order_num,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        data: { auth_token: token },
        success: (res) => {
          wx.hideLoading();
          if (res.data.status == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.wxpay();
          } else if (res.data.code == 0) {
            let orderId = res.data.data.order_num;
            let money = (parseFloat(res.data.data.order_total)*100).toFixed(0);
            that.thirdPay(orderId, money); 
          }else{
            wx.showModal({
              title: '提示',
              content: res.data.msg,
            })
            return false;
          }
        },
        fail: (res) => {
          wx.showToast({
            title: '获取借口失败',
            duration: 2000
          })
        }
      })

    })

  },

  thirdPay:function(orderId,money){
    var that = this;
    wx.cloud.callFunction({
      name: "pay",
      data: {
        orderId: orderId,
        money: money
      },
      success(res) {
        console.log("获取成功", res)
        that.goPay(res.result, orderId);
      },
      fail(err) {
        console.log("获取失败", err)
      }
    })
  },
  

  //微信支付
  goPay(payData, orderId) {
    wx.requestPayment({
      timeStamp: payData.timeStamp,
      nonceStr: payData.nonceStr,
      package: payData.package,
      signType: 'MD5',
      paySign: payData.paySign,
      success(res) {
        wx.navigateTo({
          url: "../success/success?order_num=" + orderId
        })
        
      },
      fail(res) {
        console.log("支付失败", res)
      }
    })
  },

  onLoad: function (options) {
    this.getcarts();
    
  },
  
  onShareAppMessage: function () {
    app.globalData.uid = app.globalData.uid || wx.getStorageSync('uid') || 0;
    return {
      title: '深圳智汇创想科技有限责任公司员工福利购!',
      path: '/pages/index/index?fid=' + app.globalData.uid
    }
  }
})